//
//  BookCityViewController.m
//  WNZSSQ
//
//  Created by JDKJ on 2018/12/3.
//  Copyright © 2018年 JDKJ. All rights reserved.
//

#import "BookCityViewController.h"
#import "FSPageContentView.h"
#import "FSSegmentTitleView.h"
#import "SMPagerScrollView.h"
#import "BookCityView.h"
#import "BookCitySearchViewController.h"
#import "UserNavigationController.h"
#import "BookDetalViewController.h"
#import "BookCityChildViewController.h"

@interface BookCityViewController ()<FSPageContentViewDelegate,FSSegmentTitleViewDelegate,UICollectionViewDataSource,UICollectionViewDelegate>

//导航栏上的
@property(nonatomic,weak)FSSegmentTitleView *titleView;

@property(nonatomic,weak)SMPagerScrollView *contentView;



@end

@implementation BookCityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"";
    
    [self creatUI];
    
    // 初始化所有子控制器
    [self setupChildVces];
    
    // 底部cententView
    [self setupContentView];
    
}

/**
 *  初始化所有子控制器
 */
- (void)setupChildVces
{
    
    
    for (int i = 0; i < 4; i ++) {
        
        BookCityChildViewController *childVC = [[BookCityChildViewController alloc]init];
        
        [self addChildViewController:childVC];
    }
    
    
}


/**
 *  底部cententView
 */
- (void)setupContentView
{
    // 不要自动调整inset
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    SMPagerScrollView *contentView = [[SMPagerScrollView alloc] init];
    contentView.frame = CGRectMake(0, 0, SCREENWIDTH, SCREEN_HEIGHT  - adaptNavagationBar -adaptTabBar);
    contentView.delegate = self;
    contentView.pagingEnabled = YES;
    [self.view addSubview:contentView];
    contentView.contentSize = CGSizeMake(contentView.width * self.childViewControllers.count, contentView.height);
    self.contentView = contentView;
    for (int i = 0; i < self.childViewControllers.count; i++) {
        
        // 取出子控制器
        UIViewController *vc = self.childViewControllers[i];
        
        vc.view.frame = CGRectMake(contentView.width * i, 0, contentView.width, contentView.height);
        
        [contentView addSubview:vc.view];
    }
    
    // 添加第一个控制器的view
    //    [self scrollViewDidEndScrollingAnimation:contentView];
}

-(void)creatUI
{
    
    //导航栏自定义
    [self showRightImageWithImageName:@"搜索白色"];
    
    FSSegmentTitleView *titleView = [[FSSegmentTitleView alloc]initWithFrame:CGRectMake(0, 0, 210, 40) titles:@[@"男生",@"女生",@"出版",@"恐怖"] delegate:self indicatorType:FSIndicatorTypeCustom] ;
    titleView.titleNormalColor = CSWhiteColor;
    titleView.titleSelectColor = CSWhiteColor;
    titleView.indicatorColor = CSWhiteColor;
    titleView.backgroundColor = CSClearColor;
    titleView.indicatorExtension = 15;
    titleView.selectIndex = 0;
    self.titleView = titleView;
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithCustomView:titleView];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    
}



#pragma mark UIScrollViewDelegate

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    
}
/**
 *  在scrollview停止滑动的时候执行
 */
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
    // 点击菜单按钮
    NSInteger index = scrollView.contentOffset.x / scrollView.width;
    self.titleView.selectIndex = index;

    
}


#pragma mark FSSegmentTitleViewDelegate

- (void)FSSegmentTitleView:(FSSegmentTitleView *)titleView startIndex:(NSInteger)startIndex endIndex:(NSInteger)endIndex
{
    
    [self.contentView setContentOffset:CGPointMake(self.contentView.width * endIndex , 0) animated:YES];
    
}

#pragma mark - 搜索
- (void)rightBtnClick
{
    
    BookCitySearchViewController *searcgVC = [[BookCitySearchViewController alloc]init];
    UserNavigationController *nav = [[UserNavigationController alloc] initWithRootViewController:searcgVC];
    [self presentViewController:nav  animated:NO completion:nil];
    
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
